import { StorehouseService } from './../../../../../../@core/services/storehouse/storehouse.service';
import { Component, Input, SimpleChanges, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig } from 'ng-devui';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';

@Component({
  selector: 'app-get-parts',
  templateUrl: './get-parts.component.html',
  styleUrls: ['./get-parts.component.scss']
})
export class GetPartsComponent {
  @Input() data: any;
  isSelected = false;
  displayTableData: any[] = [];
  tableheight = '230px';
  selectData: any=[];
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '工单号',
        fieldType: 'text',
        fixedLeft: '50px'
      },
      {
        field: 'name',
        header: '工序序号',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '工序编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '工序名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '物料编码',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '物料名称',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '单位',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '需求数量',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '已发数量',
        fieldType: 'text',
      }


    ]
  }
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'radio',
      width: '50px',
    },
    {
      field: '工单号',
      width: '150px'
    },
    {
      field: '工序编码',
      width: '150px'
    },
    {
      field: '工序序号',
      width: '150px'
    },
    {
      field: '工序名称',
      width: '150px'
    },
    {
      field: '物料编码',
      width: '150px'
    },
    {
      field: '物料名称',
      width: '150px'
    },
    {
      field: '单位',
      width: '150px'
    },
    {
      field: '需求数量',
      width: '150px'
    },
    {
      field: '已发数量',
      width: '150px'
    }

  ];
  getParts: any
  tableData = [];
  checkRows: any[] = [];
  isIconRotated = false;
  params = {
    PartNum: '',
    PartName: '',
    JobOrderNum: '',
    JobOrderStatus: '',
    JobOrderProcessStatus: '',
    JobOrderType: '',
    StartTime: '',
    EndTime: '',
    ProcessNum: '',
    ProcessName: '',
    PageNumber: 1,
    PageSize: 20
  };

  @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
  checkedIds: any;
  constructor(private StorehouseService: StorehouseService,private SharedMethodService:SharedMethodService) {

  }
  ngOnInit(): void {
    if(this.data=="生产退料单"){
      this.StorehouseService.GetReturnJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }else{
      this.StorehouseService.getIssueJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }

  }
  pageIndexChange($event: any) {
    if(this.data=="生产退料单"){
      this.StorehouseService.GetReturnJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }else{
      this.StorehouseService.getIssueJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }

  }//切换页码
  pageSizeChange($event: any) {
    if(this.data=="生产退料单"){
      this.StorehouseService.GetReturnJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }else{
      this.StorehouseService.getIssueJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }
  }//改变每页显示数量
  radio(event: any, rowItem: any) {
    if (event.target.checked) {
      this.isSelected = true;
      this.selectData = rowItem

    }
  }
  search() {
    if(this.data=="生产退料单"){
      this.StorehouseService.GetReturnJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }else{
      this.StorehouseService.getIssueJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }
  }
  reset() {
    this.params = {
      PartNum: '',
      PartName: '',
      JobOrderNum: '',
      JobOrderStatus: '',
      JobOrderProcessStatus: '',
      JobOrderType: '',
      StartTime: '',
      EndTime: '',
      ProcessNum: '',
      ProcessName: '',
      PageNumber: 1,
      PageSize: 20
    };
    if(this.data=="生产退料单"){
      this.StorehouseService.GetReturnJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }else{
      this.StorehouseService.getIssueJobOrderProcessParts(this.params).subscribe(
        (data: any) => {
          this.tableData = data.items
          this.pager.total = data.totalCount
        }
      )
    }
  }
  cleared(){
    this.SharedMethodService.cleared(this.checkedIds,this.tableData,this.datatable).subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }){
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable)
  }//单选
  getCheckedRows() {
    this.SharedMethodService.getCheckedRowsData(this.datatable).subscribe((data:any)=>{
      this.selectData=data;
      console.log(this.selectData)
    })
  }//获取选中行
}
